package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.filter.RoomUserFilter;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.task.dao.RoomUserDao;
import net.huashitong.supervision.task.entity.RoomUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomUserService extends StringPKService<RoomUserEntity> {

    @Autowired
    private RoomUserDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<RoomUserEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(RoomUserEntity obj) {
        obj.setSysId(tableService.updateMaxSysId("t_sys_room_user",null));
    }

    /**
     * 根据科室id 和 用户Id 获取唯一的用户
     * @param roomId
     * @param userId
     * @return
     */
    public  RoomUserEntity getUserByRoomIdAndUserId(String roomId,String userId)
    {

        RoomUserFilter filter = new RoomUserFilter();
        filter.setRoomId(roomId);
        filter.setUserId(userId);
        filter.setValid(1);
        List<RoomUserEntity> ListUsers = dao.queryFilter(filter);
        if(ObjectUtils.isBlank(ListUsers))
        {
            return null;
        }
        if(ListUsers.size()>1) {
            return null;
        }


        return ListUsers.get(0);
    }

    public List<RoomUserEntity> getUsersByRoomId(String roomId)
    {

        RoomUserFilter filter = new RoomUserFilter();
        filter.setRoomId(roomId);
        filter.setValid(1);
        List<RoomUserEntity> lists = dao.queryFilter(filter);
        return lists;
    }

}
